<?php
namespace app\common\exception;

use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\facade\Config;

class Http extends Handle
{

	public function render(Exception $e)
	{
		// 参数验证错误
		if ($e instanceof ValidateException) {
			return json($e->getError(), 422);
		}

		// 请求异常
		if ($e instanceof HttpException && request()->isAjax()) {
			return response($e->getMessage(), $e->getStatusCode());
		}

		//TODO::开发者对异常的操作
		//可以在此交由系统处理
		if(!Config::get('app_debug')){
			$result = [
				'code' => $e->getStatusCode(),
				'msg'  => $e->getMessage(),
				'time' => time(),
				'data' => [],
			];
			return json($result);
		}

		return parent::render($e);
	}

}